﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.DAL;

public partial class Police_Select_ApproveSelects : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Guid> delSelects = new List<Guid>();
            ViewState["Selects"] = delSelects;
            ListPageContral1.SetFirstPage();
            BindGV(0);

        }


    }

    void SaveSelect()
    {
        List<Guid> Selects = ViewState["Selects"] as List<Guid>;
        foreach (GridViewRow row in UserGrid.Rows)
        {
            Label lblID = row.FindControl("lblID") as Label;
            Guid id = new Guid(lblID.Text);
            CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
            if (CheckItem.Checked)
            {
                if (!Selects.Contains(id))
                {
                    Selects.Add(id);
                }
            }
            else
            {
                if (Selects.Contains(id))
                {
                    Selects.Remove(id);
                }
            }
        }
        ViewState["Selects"] = Selects;
    }

    public void BindGV(int pageIndex)
    {
        //调入时不显示联系人人人

        if (rblTpye.SelectedValue == "1")
        {
            UserGrid.Columns[8].Visible = false;
        }
        else
        {
            UserGrid.Columns[8].Visible = true;
        }
        if (rblTpye.SelectedValue == "2")
        {
            UserGrid.Columns[9].Visible = false;
            UserGrid.Columns[10].Visible = true;
        }
        else
        {
            UserGrid.Columns[9].Visible = true;
            UserGrid.Columns[10].Visible = false;
        }

        int State = 0;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400301"))//警务处长
        {
            State = 1;
        }
        else if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400302"))//参谋长
        {
            State = 2;
        }
        UserGrid.Caption = "<span>调动人员审核</span>";
        ListPageContral1.ItemCount = Select.GetAproveCount("", Convert.ToInt32(rblTpye.SelectedValue), State - 1);
        UserGrid.DataSource = Select.GetAproveList("", Convert.ToInt32(rblTpye.SelectedValue), State - 1, pageIndex, ListPageContral1.PageSize).ToList();
        UserGrid.DataBind();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        SaveSelect();
        BindGV(PageIndex);
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            TranslateSelect select = e.Row.DataItem as TranslateSelect;
            Label lblRank = e.Row.FindControl("lblRank") as Label;
            lblRank.Text = select.Rank.Trim() == "" ? "" : Yipusoft.Common.BLL.CodeItem.GetCodeItem(select.Rank).ItemName;
            CheckBox CheckItem = e.Row.FindControl("CheckItem") as CheckBox;
            List<Guid> Selects = ViewState["Selects"] as List<Guid>;
            if (Selects.Contains(select.Id))
            {
                CheckItem.Checked = true;
            }
            else
            {
                CheckItem.Checked = false;
            }
        }
    }
    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void lkbCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ApproveList.aspx?gid=" + Request["id"]);
    }

    //审核完成
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        SelectDataContext db = new SelectDataContext();
        int State = 0;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400301"))//警务处长
        {
            State = 1;
        }
        else if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400302"))//参谋长
        {
            State = 2;
        }
        SaveSelect();
        List<Guid> Selects = ViewState["Selects"] as List<Guid>;
        List<SelectLogs> logs = new List<SelectLogs>();
        foreach (Guid id in Selects)
        {
            SelectLogs log = new SelectLogs() { Id = Guid.NewGuid(), State = State, SelectId = id, SetDate = DateTime.Now, SetUserId = User.Identity.Name };
            logs.Add(log);
        }
        if (logs.Count > 0)
        {
            db.SelectLogs.InsertAllOnSubmit(logs);

        }

        List<SelectLogs> deletes = (from o in db.SelectLogs where o.State == State - 1 && !Selects.Contains(o.SelectId.Value) && o.TranslateSelect.TanslateType == Convert.ToInt32(rblTpye.SelectedValue) select o).ToList();
        db.SelectLogs.DeleteAllOnSubmit(deletes);
        db.SubmitChanges();

        ListPageContral1.SetFirstPage();
        BindGV(0);

    }
    protected void rblTpye_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Guid> Selects = ViewState["Selects"] as List<Guid>;
        Selects.Clear();
        ViewState["Selects"] = Selects;
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }

}
